﻿
Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_ImageAdver_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Bind2Select()
            txtRootImageUpload.Text = MainDomain & "uploads/"
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "abc", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "abc", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If


            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub Bind2Select()
        Dim lstStatus As New DataSet
        lstStatus.ReadXml(MapPath("../../xml/ImagesAdverSubject.xml"))
        Dim i As Integer
        'hien thi state and stateID hien thoi
        For i = 0 To lstStatus.Tables("Subject").Rows.Count - 1
            ddlSubject.Items.Add(New ListItem(lstStatus.Tables("Subject").Rows(i)("SubjectName").ToString, lstStatus.Tables("Subject").Rows(i)("SubjectID").ToString))
        Next

        Dim TextAll = "--- Chọn ---"


        Dim item As New ListItem(TextAll, 0)
        ddlSubject.Items.Insert(0, item)


    End Sub


    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As ImagesAdver = (From q In data.ImagesAdvers Where q.ID = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                txtTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                txtDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
                imgUpload.ImageUrl = IIf(IsNothing(p.SRC) = True, "", p.SRC)
                hidImagePath.Text = IIf(IsNothing(p.SRC) = True, "", p.SRC)
                txtUrl.Text = IIf(IsNothing(p.URL) = True, "", p.URL)
                ddlSubject.SelectedValue = IIf(IsNothing(p.Subject) = True, "", p.Subject)

                txtSort.Text = IIf(IsNothing(p.Sort) = True, "", p.Sort)

            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As ImagesAdver = (From q In data.ImagesAdvers Where q.ID = hidID.Value).SingleOrDefault
            Try
                p.ID = hidID.Value
                p.Title = txtTitle.Text.Trim()
                p.Description = txtDescription.Text.Trim()
                p.SRC = hidImagePath.Text.Trim()
                p.URL = txtUrl.Text.Trim()

                p.Modified = Now
                p.Modifier = Session("LanguageId")
                p.Sort = txtSort.Text.Trim()
                p.Subject = ddlSubject.SelectedValue
                data.SaveChanges()
                'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Imagesadver/Detail.aspx?Id=" + p.ID.ToString + "';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
